package com.teo.opengl.tuts;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.R.integer;

public class Square {
    // Our vertices.
	/*
    private float mVertices[] = {
                  -2.0f,  0.0f, 0.0f,  // 0
                  -1.0f,  1.0f, 0.0f,  // 1
                   0.0f,  2.0f, 0.0f,  // 2
                   1.0f,  1.0f, 0.0f,  // 3
                   2.0f,  0.0f, 0.0f,  // 4
                   1.0f, -1.0f, 0.0f,  // 5
                   0.0f, -2.0f, 0.0f,  // 6
                   0.0f, 0.0f, -1.0f,  // 77
                   
            };
    */
    // The order we like to connect them.
    
    //private short[] mIndices = { 0, 1, 7, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 0, 1 };
    
	
	 // Our vertices.
    private float mVertices[] = {
                   0.0f,  0.0f, 0.0f,
    			   0.5f,  0.0f, 0.0f,   
                   0.6f,  0.2f, 0.0f,   
                   0.7f,  0.3f, 0.0f,   
                   0.6f,  0.4f, 0.0f,  
                   0.4f,  0.6f, 0.0f,
                   0.2f,  0.5f, 0.0f,
                   0.1f,  0.3f, 0.0f
                   
            };
    
    // The order we like to connect them.
    private short[] mIndices = { 0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0};

	
	
	
    // Our vertex buffer.
    private FloatBuffer mVertexBuffer;

    // Our index buffer.
    private ShortBuffer mIndexBuffer;
    
    public Square() {

    }
    
    /**
     * This function draws our square on screen.
     * @param gl
     */
    public void draw(GL10 gl) {
    	
        // a float is 4 bytes, therefore we multiply the number if 
        // vertices with 4.
        ByteBuffer vbb = ByteBuffer.allocateDirect(mVertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(mVertices);
        mVertexBuffer.position(0);
        
        // short is 2 bytes, therefore we multiply the number if 
        // vertices with 2.
        ByteBuffer ibb = ByteBuffer.allocateDirect(mIndices.length * 2);
        ibb.order(ByteOrder.nativeOrder());
        mIndexBuffer = ibb.asShortBuffer();
        mIndexBuffer.put(mIndices);
        mIndexBuffer.position(0);
    	
            // Counter-clockwise winding.
            gl.glFrontFace(GL10.GL_CCW);
            // Enable face culling.
            gl.glEnable(GL10.GL_CULL_FACE);
            // What faces to remove with the face culling.
            gl.glCullFace(GL10.GL_BACK);
            
            // Enabled the vertices buffer for writing and to be used during 
            // rendering.
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            // Specifies the location and data format of an array of vertex
            // coordinates to use when rendering.
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
            
            gl.glDrawElements(GL10.GL_LINE_LOOP, mIndices.length, 
                            GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
            
            // Disable the vertices buffer.
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            // Disable face culling.
            gl.glDisable(GL10.GL_CULL_FACE);
            

    }
    
}
